package com.happyrecyclingstation.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.happyrecyclingstation.dto.AdminUserQueryDto;
import com.happyrecyclingstation.dto.UserDto;
import com.happyrecyclingstation.entity.User;
import com.happyrecyclingstation.mapper.UserMapper;
import com.happyrecyclingstation.service.UserService;
import com.happyrecyclingstation.utils.CurrentHolder;
import com.happyrecyclingstation.vo.AdminUserVO;
import com.happyrecyclingstation.vo.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper  userMapper;

    /**
     * 管理查询用户列表
     * @param queryParam
     * @return
     */
    @Override
    public PageResult<AdminUserVO> getUser(AdminUserQueryDto queryParam) {
        // 1. 分页查询
        PageHelper.startPage(queryParam.getCurrentPage(), queryParam.getPageSize());
        Page<AdminUserVO> page = userMapper.selectUserList(queryParam);

        // 2. 处理手机号脱敏
        List<AdminUserVO> userList = page.getResult();
        for (AdminUserVO user : userList) {
            String phone = user.getPhone();
            if (phone != null && phone.length() == 11) {
                // 将中间4位替换为****
                user.setPhone(phone.substring(0, 3) + "****" + phone.substring(7));
            }
        }

        // 3. 返回处理后的结果
        return new PageResult<>(page.getTotal(), userList);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public User getUserById(Integer id) {
        User user = userMapper.selectById(id);
        user.setPassword(null);
        return user;
    }

    /**
     * 启用或禁用用户
     * @param status
     * @param id
     */
    @Override
    public void startOrStopUser(Short status, Integer id) {
        User user = User.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .build();
        userMapper.update(user);
    }

    /**
     * 判断用户是否关联企业
     * @return
     */
    @Override
    public Boolean isRelated() {
        // 从LocalThread中获取用户id
        Integer id = CurrentHolder.getCurrentId();
        Integer companyId = userMapper.isRelated(id);
        if (companyId != null) {
            return true;
        }
        return false;
    }

    /**
     * 更新用户信息
     * @param userDto
     */
    @Override
    public void update(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        user.setId(CurrentHolder.getCurrentId());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    /**
     * 修改密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @Override
    public Boolean updatePwd(String oldPwd, String newPwd) {
        //先验证旧密码是否正确
        Integer id = CurrentHolder.getCurrentId();
        User user = userMapper.selectById(id);
        if (!BCrypt.checkpw(oldPwd, user.getPassword())){
            return false;
        }
        //修改密码
        User u = new User();
        u.setId(id);
        u.setPassword(BCrypt.hashpw(newPwd, BCrypt.gensalt()));
        userMapper.update(u);
        return true;
    }
}
